package com.tl.commonlibrary.ui.adapter;

import android.content.Context;
import android.support.annotation.LayoutRes;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by a on 2015/4/27.
 */
public abstract class IAdapter<T> extends BaseAdapter implements View.OnClickListener {
    protected List<T> mDatas;
    protected LayoutInflater mInflater;
    protected int[] mLayoutIds;
    protected Context context;
    private int bindPage = Integer.MIN_VALUE;//当前adapter绑定的是哪个界面，或者activity

    public IAdapter(Context context, List<T> datas, @LayoutRes int layoutId) {
        this.context = context;
        this.mDatas = datas;
        this.mInflater = LayoutInflater.from(context);
        mLayoutIds = new int[1];
        this.mLayoutIds[0] = layoutId;
    }

    public IAdapter(Context context, List<T> datas, int[] mLayoutIds) {
        this.context = context;
        this.mDatas = datas;
        this.mInflater = LayoutInflater.from(context);
        this.mLayoutIds = mLayoutIds;
    }

    public void addData(List<T> datas) {
        if (datas == null) {
            return;
        }
        if (mDatas == null) {
            mDatas = new ArrayList<>();
        }
        mDatas.addAll(datas);
        notifyDataSetChanged();
    }

    public void setDatas(List<T> datas) {
        if (mDatas != null) {
            mDatas.clear();
            mDatas.addAll(datas);
        } else {
            mDatas = datas;
        }

        notifyDataSetChanged();

    }

    public void addData(T data) {
        if (data == null) {
            return;
        }
        if (mDatas == null) {
            mDatas = new ArrayList<>();
        }
        mDatas.add(data);
        notifyDataSetChanged();
    }

    public List<T> getDatas() {
        return mDatas;
    }

    public void removeData(T data) {
        if (mDatas != null) {
            mDatas.remove(data);
            notifyDataSetChanged();
        }
    }

    public void removeData(int position) {
        if (mDatas != null && position < mDatas.size() && position >= 0) {
            mDatas.remove(position);
            notifyDataSetChanged();
        }
    }

    public void clear() {
        if (mDatas != null) {
            mDatas.clear();
            notifyDataSetChanged();
        }
    }

    public int getBindPage() {
        return bindPage;
    }

    public IAdapter<T> setBindPage(int bindPage) {
        this.bindPage = bindPage;
        return this;
    }

    public int getDataSize() {
        return mDatas != null ? mDatas.size() : 0;
    }

    public boolean isDataEmpty() {
        return getDataSize() > 0 ? false : true;
    }

    @Override
    public int getCount() {
        return mDatas != null ? mDatas.size() : 0;
    }

    @Override
    public T getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {
        return mLayoutIds.length > 1 ? mLayoutIds.length : 1;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        int type = getItemViewType(position);
        ViewHolder viewHolder = ViewHolder.get(context, mLayoutIds[type], position, convertView, parent);
        getView(viewHolder, getItem(position), position, viewHolder.getConvertView(), convertView != null, type, parent);
        return viewHolder.getConvertView();
    }

    /**
     * @param viewHolder   all the views attached on layout item
     * @param entity       current bean/entity by the position
     * @param position
     * @param convertView  the layout item
     * @param isConvert    true 当前过来的ConvertView是重复利用的，反之
     * @param itemViewType 当{@link #getItemViewType(int)}多类型处理时，返回当前convertView需要展示的类型
     * @param parent       the ListView
     */
    protected abstract void getView(ViewHolder viewHolder, T entity, int position, View convertView, boolean isConvert, int itemViewType, ViewGroup parent);
}
